package com.fortune.controller;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.fortune.dal.dao.CustDAO;
import com.fortune.dal.model.Cust;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("register")
public class RegisterController {

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private CustDAO custDAO;

//    @RequestMapping(value = "", method = RequestMethod.GET)
//    public ModelAndView indexOfAll() throws Exception {
//        ModelAndView mv = new ModelAndView("login");
//        return mv;
//    }

//    @RequestMapping(value = "/doregister", method = RequestMethod.POST)
//    @ResponseBody
//    public BaseResp doLogin(@RequestBody Cust cust) throws BusinessException {
//        BaseResp resp = new BaseResp();
//        try {
//            // 校验 user 参数是否为空
//            if(StringUtils.isBlank(cust.getPhone())) {
//                throw new BusinessException("手机号为空") ;
//            }
//            if(StringUtils.isBlank(cust.getPassword())){
//                throw new BusinessException("密码是空");
//            }
//            // 先检查下 username 是否存在
//            Cust param = new Cust();
//            param.setPhone(cust.getPhone());
//            param.setPassword(cust.getPassword());
//            List<Cust> custs = custDAO.queryByModel(param) ;
//            if(ListUtils.isNotEmpty(custs)) {
//                throw new BusinessException("用户已存在") ;
//            }
//            custDAO.insert(cust);
//            // 将用户放入回话中
////            User u = users.get(0) ;
////            String key = "user" ;
////            httpSession.setAttribute(key,u);
////            List<Object> orders = null ;
//        } catch (Exception ex) {
//            resp.setSuccess(false);
//            resp.setMsg(ex.getMessage());
//        }
//        return resp;
//    }


    @RequestMapping(value = "/addpersoninfo", method = RequestMethod.GET)
    public ModelAndView indexOfAllOne() throws Exception {
        ModelAndView mv = new ModelAndView("addpersoninfo");
        return mv;
    }

    @RequestMapping(value = "/checkphone", method = RequestMethod.POST)
    public BaseResp checkPhone(@RequestBody Cust cust) throws Exception {
        BaseResp resp = new BaseResp();
        try {
            if (StringUtils.isBlank(cust.getPhone())) {
                throw new BusinessException("输入手机号不能为空");
            }


            List<Cust> custList = custDAO.queryByModel(cust);
            if (ListUtils.isNotEmpty(custList)) {
                throw new BusinessException("该手机号已注册,可直接登录");
            }

            //检测验证码
            if(StringUtils.isBlank(cust.getValidateCode())){
                throw new BusinessException("验证码为空");
            }
            String clientCheckCode = cust.getValidateCode();
            System.out.println(clientCheckCode);
            String serverCheckCode = (String)httpSession.getAttribute("checkcode");
            System.out.println(serverCheckCode);

            if(!(clientCheckCode.equals(serverCheckCode))){
                throw new BusinessException("验证码错误");
            }

            Cust param = new Cust();
            param.setPhone(cust.getPhone());
            custDAO.insert(param);
            httpSession.setAttribute("phone",cust.getPhone());


        }catch(Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }


    @RequestMapping(value = "/setregpassword", method = RequestMethod.GET)
    public ModelAndView indexOfAllTwo() throws Exception {
        ModelAndView mv = new ModelAndView("setregpassword");
        return mv;
    }


    @RequestMapping(value = "/addregpassword", method = RequestMethod.POST)
    public BaseResp setPassword(@RequestBody Cust cust) throws Exception {
        BaseResp resp = new BaseResp();
        try{
            if(StringUtils.isBlank(cust.getPassword())){
                throw new BusinessException("设置密码不能为空");
            }

            Cust param = new Cust();
            param.setPhone((String)httpSession.getAttribute("phone"));
            List<Cust> list = custDAO.queryByModel(param);
            if(ListUtils.isEmpty(list)){
                throw new BusinessException("未找到用户信息");
            }
            Cust cust1 = list.get(0);
            cust1.setPassword(cust.getPassword());
            custDAO.updateById(cust1);

        }catch(Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }


    @RequestMapping(value = "/addname", method = RequestMethod.POST)
    public BaseResp addName(@RequestBody Cust cust) throws Exception {
        BaseResp resp = new BaseResp();
        try{

            System.out.println(cust.getName());
            if(StringUtils.isBlank(cust.getName())){
                throw new BusinessException("姓名填写不能为空");
            }
            Cust param = new Cust();

            param.setPhone((String)httpSession.getAttribute("phone"));
            List<Cust> list = custDAO.queryByModel(param);
            if(ListUtils.isEmpty(list)){
                throw new BusinessException("未找到用户信息");
            }
            Cust cust1 = list.get(0);

            cust1.setName(cust.getName());
            custDAO.updateById(cust1);

        }catch(Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }
}
